package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class GetSpecialPracticeController extends BaseController {
	
	private static Logger logger = LoggerFactory.getLogger(GetSpecialPracticeController.class);

	@Autowired
	private BizDao bizDao;
	
	@Autowired
	private BizService bizService;
	
	/**
	 * 获取专项分类
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getSpecialSecondConfig", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getSpecialSecondConfig(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 科目类型
			String subjectId = request.getParameter("subjectId"); 
			//学驾类型
			String driverType = request.getParameter("driverType"); 
			if(subjectId != null && !subjectId.isEmpty()
					&& driverType != null && !driverType.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("driverType", driverType);
				//获取专项分类列表
				response = bizService.getSpecialSecondConfig(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("科目类型不能为空...");
			}

		} catch (Exception e) {
			logger.info("getSpecialSecondConfig:"+ e.toString());
			response.setReturnMsg(1,"获取分类信息失败！");
		} finally {
			return response.toJSON();
		}
	}

	/**
	 * 学员端获取专项练习题型
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getSpecialTypeQuestion", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getSpecialTypeQuestion(HttpServletRequest request) {
		
		RestResponse response = new RestResponse();
		
		try {
			//专项练习模块类型(1试题类型2素材类型3知识点类型)
			String specialType = request.getParameter("specialType");
			//专项练习题类型
			String questionType = request.getParameter("questionType");
			//科目类型
			String subjectId = request.getParameter("subjectId");
			//学驾类型
			String driverType = request.getParameter("driverType");
			//用户ID
			String userId = request.getParameter("userId");
			if(specialType != null && !specialType.isEmpty()
					&& questionType != null && !questionType.isEmpty()
					&& subjectId != null && !subjectId.isEmpty()
					&& driverType != null && !driverType.isEmpty()
					&& userId != null && !userId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("specialType", specialType);
				paramsMap.put("questionType", questionType);
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("driverType", driverType);
				paramsMap.put("userId", userId);
				paramsMap.put("imgUrl", Config.getImageStudyAddress());
				//查询试题
				response = bizService.getSpecialTypeQuestion(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空！");
			}
		} catch (Exception e) {
			response.setErrorcode(1);
			response.setMessage("获取失败！");
			logger.info("getSpecialTypeQuestion" + e.toString());
		} finally {
			return response.toJSON();
		}
	}
}
